package com.chen.pfm.slice;

import com.chen.pfm.DateStatisticsAbility;
import com.chen.pfm.ResourceTable;
import com.chen.pfm.entity.DisplayBill;
import com.chen.pfm.provider.DisplayBillProvider;
import com.chen.pfm.utils.MyToast;
import com.chen.pfm.utils.MyUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;


public class AssetManagementSlice extends AbilitySlice {

    private static String CurrentClassName = "AssetManagementSlice";
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x2022001, CurrentClassName);
    List<DisplayBill> displayBills;
    DisplayBillProvider displayBillProvider;
    ListContainer listContainer;
    Text subtitle;
    DisplayBill item;//触发账单的元素
    int itemPosition;//触发事件的元素项位置
    DisplayBill displayBill;//单一被处理的
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_assets_management);
        //返回按钮监听
        findComponentById(ResourceTable.Id_goback_btn).setClickedListener((component) -> {
            terminate();
        });

        // 获取ListContainer
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_assets_ListContainer);
        subtitle = (Text) findComponentById(ResourceTable.Id_subtitle);
        subtitle.setText("我的资产");
        try {
            displayBills = MyUtil.queryDisplayTotalAsset(this);

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        if(displayBills==null){
            HiLog.error(LABEL,"资产渲染数据为空！");
            MyToast.showMyToast(this,"资产渲染数据为空！");
            return;
        }
        FulshData();
    }


    /**
     *
     */
    public void FulshData() {
        getUITaskDispatcher().asyncDispatch(()->{
            //需要的数据
            HiLog.error(LABEL, "计算得到的值：" + displayBills.toString());
            displayBillProvider=new DisplayBillProvider(displayBills,this);
            listContainer.setItemProvider(displayBillProvider);
            //点击进入详情页面
            listContainer.setItemClickedListener((container, component, i, id) -> {
                item = (DisplayBill) listContainer.getItemProvider().getItem(i);
                itemPosition = i;
                if(item==null){
                    return;
                }
                //跳转
                Intent intent = new Intent();
                intent.setParam("Aid", item.getAid());
                presentForResult(new BillDetailsSlice(), intent, 1004);

            });
        });
    }
//返回触发事件
@Override
protected void onResult(int requestCode, Intent resultIntent) {
    super.onResult(requestCode, resultIntent);
    if (requestCode == 1004) {
        if (resultIntent != null) {
            String op = resultIntent.getStringParam("op");
            if (op == null) {
                return;
            }
            if (op.equals("del")) {
                displayBills.remove(itemPosition);
                //数据改变监听事件
                listContainer.setBindStateChangedListener(new Component.BindStateChangedListener() {
                    @Override
                    public void onComponentBoundToWindow(Component component) {
                        // ListContainer初始化时数据统一在provider中创建，不直接调用这个接口；
                        // 建议在onComponentBoundToWindow监听或者其他事件监听中调用。
                        displayBillProvider.notifyDataSetItemRemoved(itemPosition);
                        // displayBillProvider.notifyDataChanged();
                    }

                    @Override
                    public void onComponentUnboundFromWindow(Component component) {
                    }
                });
            } else if (op.equals("update")) {
                String aid = resultIntent.getStringParam("aid");
                try {
                    displayBill = MyUtil.queryDisplayBillByAid(this, aid);
                } catch (DataAbilityRemoteException e) {
                    e.printStackTrace();
                }
                displayBills.remove(itemPosition);
                displayBills.add(itemPosition, displayBill);
                //数据改变监听事件
                listContainer.setBindStateChangedListener(new Component.BindStateChangedListener() {
                    @Override
                    public void onComponentBoundToWindow(Component component) {
                        // ListContainer初始化时数据统一在provider中创建，不直接调用这个接口；
                        // 建议在onComponentBoundToWindow监听或者其他事件监听中调用。
//                            displayBillProvider.notifyDataSetItemRemoved(itemPosition);
//                            displayBillProvider.notifyDataSetItemRangeInserted(itemPosition, 1);
                        displayBillProvider.notifyDataSetItemChanged(itemPosition);
                    }

                    @Override
                    public void onComponentUnboundFromWindow(Component component) {
                    }
                });
            } else {

            }


        }


    }
}

}
